package com.rock.sys.intercept;

import java.util.List;

import com.eova.aop.AopContext;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.rock.sys.config.RockConst;
import com.rock.sys.global.RockMetaIntercept;

import cn.hutool.core.util.StrUtil;

public class AppPageIntercept extends RockMetaIntercept {

	@Override
	public void queryBefore(AopContext ac) throws Exception {

		super.queryBefore(ac);

		//app角色处理
		// 当前APP，从attribute中取，PC端时为空，移动端时才有值
		String appId = ac.ctrl.getAttr(RockConst.ATTR_LOGINED_APP_ID);
		
		//如果app为空，说明是pc端，不做处理
		if(StrUtil.isBlank(appId)) {
			return;
		}
		
		List<Record> rs = Db.find("select * from t_s_app_role where app_id=?",appId);
		//1 如果app未配置任何角色，则说明此app不需要区分角色，所有用户都一样，返回所有页面
		if(rs.size()==0) {
			return;
		} else {
			//存在APP用户信息
			if(ac.user != null) {
				//取出APP用户角色
				String rid = Db.queryStr("select rid from t_s_app_user where user_id=?",ac.user.getStr("id"));
				
				//角色非空
				if(!StrUtil.isBlank(rid)) {
					//取出该角色分配的页面信息
					String sql = "SELECT DISTINCT rid,page_id from t_s_app_role_comps where rid=?";
					List<Record> rsp = Db.find(sql, rid);
					ac.condition = " and id in (''";
					for (int i = 0; i < rsp.size(); i++) {
//			            if(i==rsp.size()-1){
//			            	ac.condition += rsp.get(i).getStr("page_id");
//			                break;
//			            }
			            ac.condition += "," + rsp.get(i).getStr("page_id");
					}
					ac.condition += ") ";
				} else {
					//角色为空，取该app下默认角色
					String sql = "SELECT DISTINCT rid,page_id from t_s_app_role_comps,t_s_app_role r where rid=r.id and r.is_default='1' and r.app_id=?";
					List<Record> rsp = Db.find(sql, appId);
					ac.condition = " and id in (''";
					for (int i = 0; i < rsp.size(); i++) {
//			            if(i==rsp.size()-1){
//			            	ac.condition += rsp.get(i).getStr("page_id");
//			                break;
//			            }
			            ac.condition += "," + rsp.get(i).getStr("page_id");
					}
					ac.condition += ") ";
				}
			} else {
				//用户为空，取该app下默认角色
				String sql = "SELECT DISTINCT rid,page_id from t_s_app_role_comps,t_s_app_role r where rid=r.id and r.is_default='1' and r.app_id=?";
				List<Record> rsp = Db.find(sql, appId);
				ac.condition = " and id in (''";
				for (int i = 0; i < rsp.size(); i++) {
//		            if(i==rsp.size()-1){
//		            	ac.condition += rsp.get(i).getStr("page_id");
//		                break;
//		            }
		            ac.condition += "," + rsp.get(i).getStr("page_id");
				}
				ac.condition += ") ";
			}
		}
		
	}
	

    /**
     * 删除后置任务(事务内)
     * <pre>
     * ac.record    当前删除对象数据
     * -------------
     * 用法：级联删除，父对象删除时级联删除子对象，同一个事务内
     * </pre>
     */
	@Override
    public String deleteAfter(AopContext ac) throws Exception {
		String r = super.deleteAfter(ac);
		if(!StrUtil.isBlankIfStr(r)) {
			return r;
		}

		//同步删除相关数据
		String id = ac.record.getStr("id");
		//删除页面组件
		Db.delete("delete from t_s_page_comp where page_id=?", id);
		
		
		return null;
	}
}
